package com.tedu._11springsecurity.base.config;

import com.tedu._11springsecurity.service.UserDetailServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // 激活鉴权
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService  userDetailsService() {
        return new UserDetailServiceImpl();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                // 1.配置请求授权规则
                .authorizeRequests()
                .antMatchers("/api/public/**").permitAll()
                .anyRequest().authenticated()
                .and()

                // 2.配置表单登录
                .formLogin()
                .defaultSuccessUrl("/index.html")
                .permitAll()
                .and()

                // 3.配置退出登录
               .logout()
               .permitAll();
    }
}
